package basic.lessons;

public class GetDecimalValueOfHex {

	public static void main(String[] args) {
		String inputHexNumber = "1";
		while (!inputHexNumber.equalsIgnoreCase("0")){
		TextIO.putln();
		TextIO.putln("Please, enter HEX number and press \"Enter\"");
		TextIO.putln("To exit the program type 0 and press \"Enter\"");
		TextIO.putln();
		inputHexNumber = TextIO.getlnString().toUpperCase();
		TextIO.put("Decimal value is " +calculateDecValue(inputHexNumber));
		}
	}
	public static int calculateDecValue (String s){
		int value = 0;
        for ( int i = 0; i < s.length();  i++ ) {
        	if (s.charAt(i)==-1){
        	TextIO.putln("Error:  Input is not a hexadecimal number.");	
        	}
        	else
           value = value*16 + getDecValue( s.charAt(i) );
        }
        return value;
	}
	
	public static int getDecValue (char c){
		int decimalValue = 0;
		switch (c){
		default:
			decimalValue = -1;
			break;
		case '0': 
			decimalValue = 0;
			break;
		case '1': 
			decimalValue = 1;
			break;
		case '2': 
			decimalValue = 2;
			break;
		case '3': 
			decimalValue = 3;
			break;
		case '4': 
			decimalValue = 4;
			break;
		case '5': 
			decimalValue = 5;
			break;
		case '6': 
			decimalValue = 6;
			break;
		case '7': 
			decimalValue = 7;
			break;
		case '8': 
			decimalValue = 8;
			break;
		case '9': 
			decimalValue = 9;
			break;
		case 'A': 
			decimalValue = 10;
			break;
		case 'B': 
			decimalValue = 11;
			break;
		case 'C': 
			decimalValue = 12;
			break;
		case 'D': 
			decimalValue = 13;
			break;
		case 'E': 
			decimalValue = 14;
			break;
		case 'F': 
			decimalValue = 15;
			break;		
		}
		return decimalValue;
	}
}
